#include "accountarea.h"
#include "ui_accountarea.h"

AccountArea::AccountArea(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AccountArea)
{
    ui->setupUi(this);
    ui->pushButton_save->hide();
    useroc=FriendListLib::getInstance()->getUserOC();
    username=FriendListLib::getInstance()->getUserName();
    ui->lineEdit_useroc->setText(useroc);
    ui->lineEdit_username->setText(username);
    ui->label_avatar->setPixmap(AvatarLib::getInstance()->getAvatar("user"));
}

AccountArea::~AccountArea()
{
    delete ui;
}

QSize AccountArea::getSize()
{
    return this->size();
}

void AccountArea::on_lineEdit_useroc_textChanged(const QString &arg1)
{
    if(arg1==useroc)
    {
        ui->pushButton_save->hide();
        ui->lineEdit_username->setReadOnly(false);
        return;
    }
    ui->pushButton_save->show();
    ui->lineEdit_username->setReadOnly(true);

}

void AccountArea::on_lineEdit_username_textChanged(const QString &arg1)
{
    if(arg1==username)
    {
        ui->pushButton_save->hide();
        ui->lineEdit_useroc->setReadOnly(false);

        return;
    }
    ui->pushButton_save->show();
    ui->lineEdit_useroc->setReadOnly(true);

}

void AccountArea::on_pushButton_changeavatar_clicked()
{
    QString img=QFileDialog::getOpenFileName(this,"选择文件","C:","images(*.png *.jpg)");
    ui->label_avatar->setPixmap(img);
    //修改本地用户头像,并将头像文件保存到user目录
    ThreadManger::getInstance()->setUserAvatar(img);
    ThreadManger::getInstance()->refreshAvatar();
    //发送新头像到服务器。。。
    ThreadManger::getInstance()->sendUserAvatar(useroc,img);
}

void AccountArea::on_pushButton_save_clicked()
{
    QString newuseroc=ui->lineEdit_useroc->text();
    QString newusername=ui->lineEdit_username->text();
    ThreadManger::getInstance()->updateUser(useroc,newuseroc,newusername);
    if(useroc!=newuseroc)
    {
        //发送报文
        ThreadManger::getInstance()->sendUserOCUpdate(newuseroc);
        //更新缓冲区
        FriendListLib::getInstance()->setUserOC(newuseroc);
        //设置暂存新oc，关闭时重命名文件夹
        ThreadManger::getInstance()->setNewUserOC(newuseroc);
        useroc=newuseroc;
    }
   if(username!=newusername)
   {
        ThreadManger::getInstance()->sendUserNameUpdate(useroc,newusername);
        FriendListLib::getInstance()->setUserName(newusername);
        username=newusername;
   }
    ui->pushButton_save->hide();
}

void AccountArea::on_pushButton_exituser_clicked()
{
    //立即将新聊天记录写入数据库
    ThreadManger::getInstance()->writeChatBufNow();
    //刷新数据库中的最新消息
    ThreadManger::getInstance()->refreshLastestDB(ChatWidget::getChangeSet());
    //发送退出登录报文
    ThreadManger::getInstance()->logout(useroc);
    //清除所有缓冲区内容
    ThreadManger::getInstance()->clearLib();
    ThreadManger::getInstance()->deleteDatabase();
    ThreadManger::getInstance()->updateUserOcFile();
    FriendApplyLib::getInstance()->resetNewCount();
    //界面跳转
    PageManger::logout();
    SettingsPageManger::deletePage();
    ThreadManger::getInstance()->flushSettings();
}
